package com.example.simplemusicplayer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MusicService extends Service {
    private static final String TAG = "MusicService";
    public MediaPlayer mediaPlayer;  //创建MediaPlayer对象
    private TextView songDuration;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    class MyBinder extends Binder {

        public void play(Context context) {
            try {
                if (mediaPlayer == null) {
                    mediaPlayer = MediaPlayer.create(context, R.raw.kebukeyi);
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mediaPlayer.start();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void pause() {
            if (mediaPlayer != null & mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            } else if (mediaPlayer != null & (!mediaPlayer.isPlaying())) {
                mediaPlayer.start();
            }
        }

        public int getCurrentProgress() {
            if (mediaPlayer != null & mediaPlayer.isPlaying()) {
                return mediaPlayer.getCurrentPosition() * 100 / mediaPlayer.getDuration();
            } else if (mediaPlayer != null & (!mediaPlayer.isPlaying())) {
                return mediaPlayer.getCurrentPosition() * 100 / mediaPlayer.getDuration();
            }
            return 0;
        }

        public String getCurrentTime() {
            String min = "";
            String sec = "";
            String currentTime = "";

            try {
                int currentMin = mediaPlayer.getCurrentPosition() / 1000 / 60;
                int cuurentSec = mediaPlayer.getCurrentPosition() / 1000 % 60;

                if (currentMin < 10) {
                    min = "0" + currentMin;
                } else {
                    min = currentMin + "";
                }
                if (cuurentSec < 10) {
                    sec = "0" + cuurentSec;
                } else {
                    sec = cuurentSec + "";
                }
                currentTime = min + ":" + sec;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return currentTime;
        }

        public String getSongDuration() {
            String durantion = "";
            try {
                String min = "";
                String sec = "";
                int durantionMin = mediaPlayer.getDuration() / 1000 / 60;
                if (durantionMin < 10) {
                    min = "0" + durantionMin;
                }
                int durantionSec = mediaPlayer.getDuration() / 1000 % 60;
                if (durantionSec < 10) {
                    sec = "0" + durantionSec;
                }
                durantion = min + ":" + sec;
                Log.d(TAG, "play: " + durantion);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return durantion;
        }
    }

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public void onDestroy() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        super.onDestroy();
    }
}
